package com.aspire.nm.component.miniServerDemo.server;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

import com.aspire.nm.component.miniServer.Controll;
import com.aspire.nm.component.miniServer.annotation.Controller;
import com.aspire.nm.component.miniServer.annotation.Controller.MethodType;
import com.aspire.nm.component.miniServer.annotation.cls.Controllers;
import com.aspire.nm.component.miniServer.annotation.method.ClientIp;
import com.aspire.nm.component.miniServer.annotation.method.Header;
import com.aspire.nm.component.miniServer.annotation.method.JSessionId;
import com.aspire.nm.component.miniServer.annotation.method.Path;
import com.aspire.nm.component.miniServer.annotation.method.Req;
import com.aspire.nm.component.miniServer.annotation.method.ServerConfig;
import com.aspire.nm.component.miniServer.annotation.method.Session;
import com.aspire.nm.component.miniServer.handle.Redirect;
import com.aspire.nm.component.miniServer.handle.ViewModel;
import com.aspire.nm.component.miniServerDemo.bean.Birthday;
import com.aspire.nm.component.miniServerDemo.bean.Person;
import com.aspire.nm.component.miniServerDemo.bean.Student;

@Controllers(path="$ExpensivelImpl.path")
public class ControllImpl implements Controll {

    /**
     * welcomePage 
     * http://127.0.0.1:7070/
     */
    /**
     * 404Page 
     * http://127.0.0.1:7070/aaa.rar
     */
    /**
     * staticResource
     * 修改text/html等配置
     * http://127.0.0.1:7070/a.txt
     * http://127.0.0.1:7070/b.html
     */
    /**
     * downLoadStaticResource
     * http://127.0.0.1:7070/1.zip
     */
    /**
     * 403Page
     * 修改allowIpsPattern
     * http://127.0.0.1:7070/xxx.txt
     */
    
    
    
    
    
    
    
    
    
    /**
     * http://127.0.0.1:7070/demo/sayError
     */
    @Controller(timeOut=2000,desc="500异常")
    public String sayError(){
        throw new RuntimeException();
    }
    
    
    
    
    
    /**
     * http://127.0.0.1:7070/demo/hello?user=张三&age=15&iswomen=true&c=f
     */
    @Controller(desc="基本参数接收")
    public String hello(@ClientIp String clientIp,
            @ServerConfig(key="serverPort") String serverPort,
            @Header(key="User-Agent") String ua,
            @Req(key="user") String user,
            @Req(key="age") long age,
            @Req(key="c") char c,
            @Req(key="iswomen") boolean iswomen,
            @Session Map<String,Object> session,
            @JSessionId String jsessionId,
            @ServerConfig(key="sessionExpireSec") String sessionExpireSec){
        
        StringBuffer sb = new StringBuffer();
        sb.append("客户端IP =" + clientIp+"\r\n");
        sb.append("服务端PORT =" + serverPort+"\r\n");
        sb.append("header中ua =" + ua+"\r\n");
        sb.append("get参数user =" + user+"\r\n");
        sb.append("get参数age =" + age+"\r\n");
        sb.append("get参数c =" + c+"\r\n");
        sb.append("get参数iswomen =" + iswomen+"\r\n");
        sb.append("session超时 =" + sessionExpireSec+"\r\n");
        sb.append("jsessionId =" + jsessionId+"\r\n");
        if(session.get("count") == null){
            session.put("count", 1);
            sb.append("首次访问 " +"\r\n");
        }else{
            int count = ((Integer)session.get("count")).intValue();
            count ++;
            session.put("count", count);
            sb.append("访问次数 = "+count +"\r\n");
        }
        
        return sb.toString();
    }
    
    
    /**
     * http://127.0.0.1:7070/demo/uu1/1242/1/blog
     */
    @Controller(pathPattern = "(\\w{3,10})/(\\d*)/([1|0])/(blog)",desc="path传参")
    public String showBlog(@Path String path,
            @Path(groupIndex=1) String username1,
            @Path(groupIndex=2) int userId1,
            @Path(groupIndex=3) boolean ismen1){
        return "path  = " + path + " username : " + username1 + " userId:" + userId1 +" ismen = " + ismen1;
    }
    
    
    
    
    
    
    
    
    /**
     * http://127.0.0.1:7070/postTest.html
     */
    @Controller(pathPattern = "user/post",methodType=MethodType.POST,desc="接受Post数据")
    public String sendPostDate(@Req String postDate1,@Req byte[] data){
        return "数据处理完毕,postDate = " + postDate1+" data.length = " + data.length;
    }
    
    
    
    
    
    
    
    
    /**
     * http://127.0.0.1:7070/demo/getStudent
     * http://127.0.0.1:7070/demo/getList
     * http://127.0.0.1:7070/demo/getMap
     */
    @Controller(desc="json返回(数据结构)")
    public Student getStudent(){
        Student student = new Student(22,"张三");
        student.setBirthday(new Birthday("2001年","10月","01日"));
        student.getScore().put("语文", 100);
        student.getScore().put("数学1", 70);
        
        Student student1 = new Student(22,"李四");
        student1.setBirthday(new Birthday("2002","10","01"));
        student1.getScore().put("语文", 100);
        student1.getScore().put("数学1", 70);
        
        student.getFriends().add(student1);
        return student;
    }
    @Controller(desc="json返回(List)")
    public List<String> getList(){
        List<String> list = new ArrayList<String>();
        list.add("dd");
        list.add("ee");
        return list;
    }
    @Controller(desc="json返回(Map)")
    public Map<String,Student> getMap(){
        Map<String,Student> map = new HashMap<String,Student>();
        map.put("user1", new Student(22,"张三"));
        map.put("user2", new Student(21,"李四"));
        return map;
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    /**
     * http://127.0.0.1:7070/demo/ScoreList
     */
    @Controller(desc="模板渲染")
    public ViewModel ScoreList(){
        
        List<Student> list = new ArrayList<Student>();
        
        Student student = new Student(22,"张三");
        student.setBirthday(new Birthday("2001年","10月","01日"));
        student.getScore().put("yw", 68);
        student.getScore().put("sx", 56);
        
        Student student1 = new Student(22,"李四");
        student1.setBirthday(new Birthday("2001年","10月","01日"));
        student1.getScore().put("yw", 61);
        student1.getScore().put("sx", 78);
        
        Student student2 = new Student(22,"李四");
        student2.setBirthday(new Birthday("2001年","10月","01日"));
        student2.getScore().put("yw", 77);
        student2.getScore().put("sx", 22);
        
        list.add(student);
        list.add(student1);
        list.add(student2);
        
        HashMap<String, List<Student>> model = new HashMap<String,List<Student>>();
        model.put("studentList", list);
        return new ViewModel("a_template",model);
    }
    
    
    
    /**
     * http://127.0.0.1:7070/demo/downLoad
     */
    @Controller(desc="文件下载")
    public File downLoad(){
        String path = "G:\\svn_commons\\component\\miniServerDemo\\webapps\\123.doc";
        return new File(path);
    }
    
    
    /**
     * http://127.0.0.1:7070/demo/redirect?u=张三
     */
    @Controller(desc="redirect")
    public Redirect redirect(@Req(key="u") String u){
        return new Redirect("redirected?u="+u);
    }
    @Controller(desc="redirected")
    public String redirected(@Req(key="u") String u){
        return u;
    }
    
    
    
    /**
     * 
     * http://127.0.0.1:7070/demo/rsa
     */
    @Controller(methodType=MethodType.POST,desc="rsaFilter")
    public String rsa(@Req String postDate){
        return "收到 " + postDate;
    }
    
    
    
    
    
    
    
    /**
     * http://127.0.0.1:7070/demo/cacheMethod?cache_a=1&cache_b=2
     */
    
    @Controller(cacheTime=5000,desc="接口缓存")
    public String cacheMethod(@Req(key = "cache_a") String cache_a,@Req(key = "cache_b") String cache_b) {
        System.out.println("int cacheTime...");
        return "ok " + cache_a + " " + cache_b;
    }
    public String cacheMethod_OFKEY(@Req(key = "cache_a") String cache_a,@Req(key = "cache_b") String cache_b){
        return cache_a + " " + cache_b;
    }
    
    
    
    /**
     * http://127.0.0.1:7070/demo/cacheMethod1
     * (默认的key为class_mehtod)
     */
    @Controller(cacheTime=5000,desc="接口缓存(默认的key)")
    public String cacheMethod1(){
        System.out.println("int cacheTime...");
        return "ok ";
    }
    
    
    
    
    
    
    
    
    /**
     * http://127.0.0.1:7070/demo/timeOut
     */
    @Controller(timeOut=2000,desc="接口超时")
    public void timeOut() throws InterruptedException{
        Thread.sleep(3000);
    }
    
    
    
    
    
    
    
    
    /**
     * http://127.0.0.1:7070/demo/postJson1
     */
    @Controller(desc="接收json对象Person")
    public Person postJson1(@Req Person person){
        if(person == null){
            System.out.println("格式错误");
        }
        return person;
    }
    
    
    
    /**
     * http://127.0.0.1:7070/demo/postJson2
     */
    @Controller(desc="接收json对象Birthday")
    public Birthday postJson2(@Req Birthday birthday){
        if(birthday == null){
            System.out.println("格式错误");
        }
        return birthday;
    }
    
    
    
    
    
    
    
    
    
    
    
    
    /**
     * 
     *  http://127.0.0.1:7070/demo/lowEfficiency
     */
    private static AtomicLong count = new AtomicLong();
    @Controller(timeOut=30000,desc="低效接口")
    public void lowEfficiency() throws InterruptedException{
        System.out.println("count"+count.incrementAndGet());
        Thread.sleep(1000*1000*1000);
    }
    
    
    
    
    /**
     * 
     * http://127.0.0.1:7070/upload.html
     */
//    @Controller(desc="文件上传")
//    public String uploadLoad(){
//        return "123";
//    }
}

